package sto.android.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

/**
 * ���Žӿ�ʵ��
 * 
 * @author Don
 * 
 */
public class RongXinJNI implements StoJNI {

	private static RongXinJNI rongXinJNI;
	private Context mContext;
	private final String DEV_C40 = "C40";
	private String strDevModel;

	private RongXinJNI(Context context) {
		mContext = context;
	}

	public static RongXinJNI getInstance(Context context) {
		if (rongXinJNI == null) {
			rongXinJNI = new RongXinJNI(context);
		}
		return rongXinJNI;
	}

	@Override
	public void SetScannerOn() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerOn();
		} else {
			RX70Scanner.getInstance().Barcode_open();
		}

	}

	@Override
	public void SetScannerOff() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerOff();
		} else {
			RX70Scanner.getInstance().Barcode_Close();
		}
	}

	@Override
	public void SetScannerStart() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerStart();
		} else {
			RX70Scanner.getInstance().Barcode_StartScan();
		}

	}

	@Override
	public void SetScannerStop() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerStop();
		} else {
			RX70Scanner.getInstance().Barcode_StopScan();
		}

	}

	@Override
	public int SetScannerPara(int para, int value) {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().SetScannerPara(para, value);
		} else {
			return 0;
		}
	}

	@Override
	public int GetScannerPara(int para) {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetScannerPara(para);
		}
		return 0;
	}

	@Override
	public String SetScannerPara(String para, String value) {
		if (getDevModel().equals(DEV_C40)) {
			if(para.startsWith("0x")  || para.startsWith("0X")){
				para=para.substring(2);
			}
			if(value.startsWith("0x")  || value.startsWith("0X")){
				value=value.substring(2);
			}
			
			return RXC40Scanner.getInstance().SetScannerPara(para,value);
		}
		return null;
	}

	@Override
	public String GetScannerPara(String para) {
		if (getDevModel().equals(DEV_C40)) {
			byte[] byteArray=  RXC40Scanner.getInstance().GetScannerPara(para);
			String str=bytesToHexString(byteArray);
			return str;
			
		}
		return null;
	}

	@Override
	public void SetScannerRetriger() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerRetriger();
		} else {
			RX70Scanner.getInstance().Barcode_StartScan();
		}

	}
	public   String bytesToHexString(byte[] src){  
	    StringBuilder stringBuilder = new StringBuilder("");  
	    if (src == null || src.length <= 0) {  
	        return null;  
	    }  
	    for (int i = 0; i < src.length; i++) {  
	        int v = src[i] & 0xFF;  
	        String hv = Integer.toHexString(v);  
	        if (hv.length() < 2) {  
	            stringBuilder.append(0);  
	        }  
	        stringBuilder.append(hv);  
	    }  
	    return stringBuilder.toString();  
	}  
	
	@Override
	public void SetScannerTimerOut(int sec) {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().SetScannerTimerOut(sec);
		} 
	}

	@Override
	public boolean GetScannerIsScanning() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetScannerIsScanning();
		} 
		return false;
	}

	@Override
	public int getmScanModel() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().getmScanModel();
		} 
		return 0;
	}

	@Override
	public void SetSystemReboot() {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemReboot(mContext);
		} else {
			// /RX70Scanner.getInstance().
		}
	}

	@Override
	public void SetSystemShutdown() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemShutdown(mContext);
		} else {
			// /RX70Scanner.getInstance().
		}

	}

	@Override
	public void SetSystemTime(String mm) {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemTime(mContext, mm);
		} else {
			RX70Scanner.getInstance().SetSystemTime(mContext, mm);
		}
	}

	@Override
	public void SetMachineCode(String code) {

	}

	@Override
	public String GetMachineCode() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetMachineCode();
		} else {
			return RX70Scanner.getInstance().GetMachineCode(mContext);
		}

	}

	@Override
	public void disableKeypad() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setInterruptKeys(mContext, true);
		} else {

		}
	}

	@Override
	public void disableKeypadEnableCustom(String customstr) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					customstr);
		} else {

		}
	}

	@Override
	public void enableKeypad() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setInterruptKeys(mContext, false);
		} else {

		}
	}

	@Override
	public void enableKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyDownTone(mContext, true);
		} else {

		}
	}

	@Override
	public int GetStatusKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetKeydownTone();
		} else {
			return 0;
		}

	}

	@Override
	public void disableKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyDownTone(mContext, false);
		} else {

		}
	}

	@Override
	public void TurnOffStatusBarExpand() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOffStatusBar(mContext, false);
		} else {

		}

	}

	@Override
	public int GetStatusBarExpand() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusBarExpand();
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOnStatusBarExpand() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOffStatusBar(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOffUSBDebug() {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetUSBDebug(mContext, false);
		} else {

		}
	}

	@SuppressLint("NewApi")
	@Override
	public int GetStatusUSBDebug() {

		return Settings.Secure.getInt(
				mContext.getContentResolver(), Settings.Global.ADB_ENABLED, 0) > 0 ? 1
				: 0;
	}

	@Override
	public void TurnOnUSBDebug() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetUSBDebug(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOffInstallManager() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().InstallApkEn(mContext, false);
		} else {

		}
	}

	@Override
	public int GetStatusInstallManager() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusInstallManager();
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOnInstallManager() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().InstallApkEn(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOnScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScreenLock(mContext, true);
		} else {

		}
	}

	@Override
	public int GetStatusScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusScreenLock();
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOffScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScreenLock(mContext, false);
		} else {

		}
	}

	@Override
	public int GetStatusWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusWakeUp();
		} else
			return 0;
	}

	@Override
	public void CfgPowerKeyWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setWakeup(mContext, true);
		}
	}

	@Override
	public void CfgAnyKeyWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setWakeup(mContext, false);
		}
	}

	@Override
	public void Disable_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			if(RXC40Scanner.getInstance().GetTouchStatus()==0){
				return ;
			}
			
			RXC40Scanner.getInstance().setTouchStatusEn(mContext, false);
		}
	}

	@Override
	public void Enable_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			if(RXC40Scanner.getInstance().GetTouchStatus()==1){
				return ;
			}
			RXC40Scanner.getInstance().setTouchStatusEn(mContext, true);
		}
	}

	@Override
	public int GetStatusTouch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetTouchStatus();
		}
		return 0;
	}

	@Override
	public void StoJNI_InstallApkWithSilence(String apkPath,
			boolean runAfterFinished) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().installApkWithSilence(mContext, apkPath,
					runAfterFinished);
		}

	}

	@Override
	public void TurnOnOffGPS(boolean set) {
		if (getDevModel().equals(DEV_C40)) {
			boolean isOpened = RXC40Scanner.getInstance().isGpsOpened(mContext);
			if (isOpened == set) {
				return;
			}
			RXC40Scanner.getInstance().turnOnOffGPS(mContext, set);

		} else {

		}
	}

	@Override
	public void TurnOnOffBrowser(boolean set) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOnOffBrowser(mContext, set);
		}
	}

	@Override
	public void Disable_home_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance(). disableHomeKeys(mContext,true);
		} else {

		}
	}

	@Override
	public void Enable_home_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance(). disableHomeKeys(mContext,false);
			//RXC40Scanner.getInstance().setKeyboardEn(mContext,
				//android.view.KeyEvent.KEYCODE_HOME);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Home_screen() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetHomeKeyStatus();
		} else {
			return 0;
		}

	}

	@Override
	public void Disable_menu_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					String.valueOf(android.view.KeyEvent.KEYCODE_MENU));
		} else {

		}
	}

	@Override
	public void Enable_menu_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyboardEn(mContext,
					android.view.KeyEvent.KEYCODE_MENU);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Menu_screen() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetMenuStatus();
		} else {
			return 0;
		}
	}

	@Override
	public void Disable_back_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					String.valueOf(android.view.KeyEvent.KEYCODE_BACK));
		} else {

		}
	}

	@Override
	public void Enable_back_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyboardEn(mContext,
					android.view.KeyEvent.KEYCODE_BACK);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Back_screen() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetBackStatus();
		} else {
			return 0;
		}
	}

	private String getDevModel() {
		if (strDevModel == null || TextUtils.isEmpty(strDevModel)) {
			strDevModel = android.os.Build.MODEL;
		}
		return strDevModel;
	}

	@Override
	public void setmScanCB(ScanCallBack mScanCB) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().scanCallBack = mScanCB;
		} else {
			RX70Scanner.getInstance().scanCallBack = mScanCB;
		}

	}

	@Override
	public int getPlatform() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().getSysBuildVer();
		}
		return 0;
	}

	@Override
	public String getJniVer() {
		if (getDevModel().equals(DEV_C40)) {
		  return	RXC40Scanner.getInstance().getJniVer();
		}
		return "";
	}

	@Override
	public int getEventFuction(int var1) {
		if (getDevModel().equals(DEV_C40)) {
			switch(var1){
				case  android.view.KeyEvent.KEYCODE_MUTE :
					return StoPdaKeyEvent.KEYCODE_SCAN;

			}

		}
		return var1;

	}

	@Override
	public String getPdaBrand() { 
		return "RX";
	}

	@Override
	public void SetGPIOStatusJNI(int var1, int var2) {
	 
		
	}

}
